/* $Id:party.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "party"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "party.h"
#include "army.h"
#include "../general/general.h"

party::party() {
    name="";
    players=map<string,class person>();
    color=0;
    capital=0;
}

// Field : name

const string party::getName() const { return name; }
void party::setName(const string v) { name=v; }

// Field : players

class person& party::getPlayers(const string v) { return players[v]; }
map<string,class person>& party::listPlayers() { return players; }
void party::setPlayers(class person &v) {
    if (v.getName()=="") throw unnamed();
    players[v.getName()]=v;
}
void party::delPlayers(const string v) { players.erase(v); }
const bool party::isPlayers(const string v) const { return players.count(v)>0; }

// Field : color

const int party::getColor() const { return color; }
void party::setColor(const int v) { color=v; }

// Field : capital

class army& party::getCapital() const { if (capital==0) throw nullpointer(); return *capital; }
void party::setCapital(class army &v) { capital=&v; }

void party::save(class channel &ch) {
    ch.write("party", "");
    ch.write("n", getName());
    loopPartyPlayers(this, s_players) {
        s_players->save(ch);
    }
    ch.write("c", getColor());
    ch.write("ca", getCapital().getName());
}

void party::load(class channel &ch) {
    if (ch.key=="n" || ch.key=="name") { setName(ch.value); ch.next(); }
    while (ch.key=="person" || ch.key=="players") {
        ch.next();
        class person rec;
        rec.load(ch);
        setPlayers(rec);
    }
    if (ch.key=="c" || ch.key=="color") { setColor(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="ca" || ch.key=="capital") {
        if (!army_repo.isArmy(ch.value)) {
            dbg_state("link unknown army %s", ch.value.c_str());
            class army rec;
            rec.setName(ch.value);
            army_repo.setArmy(rec);
        }
        setCapital(army_repo.getArmy(ch.value));
        ch.next();
    }
}

void party::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<party name=\"" << xml_encode(getName()) <<
    "\" color=\"" << getColor() <<
    "\">" << endl;
    if (r<2) try {
        getCapital().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    if (r==0) {
        loopPartyPlayers(this, s_players) {
            s_players->xml(f,l+1);
        }
    }
    f << ncpy(l, "  ") << "</party>" << endl;
}

// Repository

partyRepository party_repo;

class party& partyRepository::getParty(const string v) { return partys[v]; }
void partyRepository::setParty(class party& v) {
    if (v.getName()=="") throw unnamed();
    partys[v.getName()]=v;
}
map<string,class party>& partyRepository::listParty() { return partys; }
void partyRepository::delParty(const string v) { partys.erase(v); }
const bool partyRepository::isParty(const string v) const { return partys.count(v)>0; }
void partyRepository::save(class channel &ch) {
    loopPartys(rec) {
        rec->save(ch);
    }
}

bool partyRepository::load(class channel &ch) {
    if (ch.key=="party") {
        ch.next();
        class party rec;
        rec.load(ch);
        setParty(rec);
        return true;
    }
    return false;
}
